package com.lagou.server;

import com.lagou.examutil.CommonUtil;
import com.lagou.model.User;

import java.io.*;
import java.util.LinkedList;

/**
 * @author StarryHorizon
 * @create 2022-01-07 20:55
 */
public class ServerDao {

    /**
     *本地存储学员信息文件路径
     */
    private static final String USERS_LOCAL_FILE_PATH = "./ExamSystemServer/src/userList.txt";

    /**
     *List集合存放所有学员账户信息
     */
    private LinkedList<User> userList = new LinkedList<>();

    /**
     * 当前用户
     */
    private User currentUser = new User();

    /**
     * 编程实现管理员账号和密码的校验并将结果返回
     * @param user
     * @return
     */
    public boolean serverManagerCheck(User user) {
        if ("admin".equals(user.getUserName()) && "123456".equals(user.getPassword())) {
            //存放当前的用户
            currentUser.setUserName(user.getUserName());
            currentUser.setPassword(user.getPassword());
            return true;
        }
        return false;
    }

    /**
     * 新增学员信息并返回结果
     */
    public boolean serverAddStudent(User student) {
        // 判断是否存在与新增学员相同用户名的账户
        if (userList.stream().noneMatch(user -> user.getUserName().equals(student.getUserName()))) {
            return userList.add(student);
        } else {
            return false;
        }
    }

    /**
     *删除学员信息并返回结果
     */
    public boolean serverDelStudent(User student) {
        // 遍历查找相同用户名的学员并删除
        for (User user : userList) {
            if (user.getUserName().equals(student.getUserName())) {
                return userList.remove(user);
            }
        }
        return false;
    }

    /**
     *更新学员密码信息并返回结果
     */
    public boolean serverUpdStudent(User student) {
        // 遍历查找相同用户名的学员并更新
        for (User user : userList) {
            if (user.getUserName().equals(student.getUserName())) {
                // 先删除再增加
                return userList.remove(user) && userList.add(student);
            }
        }
        return false;
    }

    /**
     *查询学员信息并返回结果
     */
    public User serverGetStudent(User student) {
        // 遍历查找相同用户名的学员并返回
        for (User user : userList) {
            if (user.getUserName().equals(student.getUserName())) {
                return user;

            }
        }
        return null;

    }


    /**
     * 导入文件的学员信息
     * @return
     */
    public void loadUsersFromDatabase() {
        //从本地文件读取所有学院账户信息到List集合中
        File file = new File(USERS_LOCAL_FILE_PATH);
        if (file.exists()) {
            ObjectInputStream ois = null;
            try {
                //建立对象输入流
                ois = new ObjectInputStream(new FileInputStream(file));
                //读取信息
                setUserList(CommonUtil.cast(ois.readObject()));
                System.out.println("学员信息如下：");
                System.out.println("--------------------------------------");
                if (null == getUserList() || getUserList().isEmpty()) {
                    System.out.println("无");
                } else {
                    getUserList().forEach(System.out::println);
                }
                System.out.println("--------------------------------------");
                System.out.println("学员信息导入成功");
                System.out.println();
            } catch (IOException  | ClassNotFoundException e) {
                e.printStackTrace();
            }  finally {
                //释放资源
                if (null != ois) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            System.out.println("学员账户信息为空");
            System.out.println();
        }
    }

    /**
     * 保存学员账户数据到文件
     * @return
     */
    public void saveUsersToDataBase() {
        //保存List集合中学员信息到本地文件
        File file = new File(USERS_LOCAL_FILE_PATH);
        ObjectOutputStream oos = null;
        try {
            //若文件不存在则创建文件
            if (!file.exists()) {
                System.out.println(file.createNewFile()?"学员信息文件创建成功" : "学员信息文件创建失败");
            }
            //建立对象输出流
            oos = new ObjectOutputStream(new FileOutputStream(file));
            //保存信息
            oos.writeObject(getUserList());
            oos.flush();
            System.out.println("已经完成信息保存");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public LinkedList<User> getUserList() {
        return userList;
    }

    public void setUserList(LinkedList<User> userList) {
        this.userList = userList;
    }

    public User getCurrentUser() {
        return currentUser;
    }

}
